/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Criptoanalisis.java
 *
 * Created on 8/09/2009, 06:12:31 PM
 */

package Vista;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import Analisis.*;
import javax.swing.JOptionPane;
/**
 *
 * @author felipe
 */
public class Criptoanalisis extends javax.swing.JFrame {

    /** Creates new form Criptoanalisis */
    public Criptoanalisis() {
        initComponents();
        hillpanel.setVisible(false);
        tKey.setEnabled(false);
        TipoCifrador.setSelectedIndex(0);
        Clave.setEditable(false);

        for(int i=0;i<26;i++){
           caracters[i]=(char) ('a' + i);
            coincidencias[i]=i;
        }

        llenarTabla(coincidencias, caracters);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        analisis = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tcifrado = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tplano = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        TipoCifrador = new javax.swing.JComboBox();
        tKey = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        hillpanel = new javax.swing.JPanel();
        parcl = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        parcf = new javax.swing.JTextField();
        Clave = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        ClaveB = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        BotonAyuda = new javax.swing.JButton();

        setTitle("Criptoanalisis");

        analisis.setText("Analisis");
        analisis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                analisisActionPerformed(evt);
            }
        });

        jLabel1.setText("Texto Cifrado");

        tcifrado.setColumns(20);
        tcifrado.setRows(5);
        jScrollPane1.setViewportView(tcifrado);

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tabla);

        tplano.setColumns(20);
        tplano.setRows(5);
        jScrollPane3.setViewportView(tplano);

        jLabel2.setText("Texto Plano");

        TipoCifrador.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Shift", "Affin", "Hill", "Viegenere", "Sustitucion", "Permutacion" }));
        TipoCifrador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TipoCifradorActionPerformed(evt);
            }
        });

        tKey.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8" }));

        jLabel3.setText("Tamaño Key");

        hillpanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Parejas de texto"));

        parcl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parclActionPerformed(evt);
            }
        });

        jLabel4.setText("Claro");

        jLabel5.setText("Cifrado");

        parcf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parcfActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout hillpanelLayout = new javax.swing.GroupLayout(hillpanel);
        hillpanel.setLayout(hillpanelLayout);
        hillpanelLayout.setHorizontalGroup(
            hillpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(hillpanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(hillpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(parcf, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(parcl, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );
        hillpanelLayout.setVerticalGroup(
            hillpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, hillpanelLayout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(parcl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(parcf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        Clave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClaveActionPerformed(evt);
            }
        });

        jLabel6.setText("Clave");

        ClaveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClaveBActionPerformed(evt);
            }
        });

        jLabel7.setText("B");

        BotonAyuda.setText("Ayuda");
        BotonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAyudaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(hillpanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(analisis)
                                    .addComponent(jLabel6))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(Clave, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(TipoCifrador, javax.swing.GroupLayout.Alignment.LEADING, 0, 110, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(tKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(ClaveB, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(69, 69, 69)))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BotonAyuda)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(analisis)
                            .addComponent(TipoCifrador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Clave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(ClaveB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(hillpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(BotonAyuda))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void analisisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analisisActionPerformed
        // TODO add your handling code here:
        Analisis a = null;
        try {
            a = new Analisis();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Criptoanalisis.class.getName()).log(Level.SEVERE, null, ex);
        }
        llenarTabla(a.contar(tcifrado.getText()), caracters);

        switch(TipoCifrador.getSelectedIndex()){

          case 0:
                tplano.setText(a.shift(tcifrado.getText()));
                Clave.setText(String.valueOf(a.getClaveShift()));


                break;
          case 1:
                tplano.setText(a.affin(tcifrado.getText()));
                a.getClaveAffinB();
                a.getClaveAffinK();
                Clave.setText(String.valueOf(a.getClaveAffinK()));
                ClaveB.setText(String.valueOf(a.getClaveAffinB()));




                break;
          case 2:

              String clave=" ";
               tplano.setText(a.hill(tcifrado.getText(),parcl.getText(), parcf.getText(), tKey.getSelectedIndex()+1));
               for(int i=0; i<tKey.getSelectedIndex()+1; i++){

                   for(int j=0; j<tKey.getSelectedIndex()+1; j++){

                      clave+=(a.getClaveHill()[i][j]%26+" , ");


                   }

               }

               Clave.setText(clave);

                break;
          case 3:
                 if(tcifrado.getText().length()<30){

                     JOptionPane.showMessageDialog(null, "El texto cifrado es muy corto");
                 }else{
                 tplano.setText(a.Vigenere(tcifrado.getText()));
                 Clave.setText(a.calKey(tcifrado.getText()));

                 }

                break;
          case 4:
               
                break;
          case 5:
               tplano.setText(a.permutacion(tcifrado.getText(),tKey.getSelectedIndex()+1));
               Clave.setText(a.getClavePermutacion());

                break;
        }
      
}//GEN-LAST:event_analisisActionPerformed

    private void TipoCifradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TipoCifradorActionPerformed
        // TODO add your handling code here:

         switch(TipoCifrador.getSelectedIndex()){

          case 0:
                hillpanel.setVisible(false);
                 tKey.setEnabled(false);
                break;
          case 1:
                 hillpanel.setVisible(false);
                 tKey.setEnabled(false);

                break;
          case 2:
                hillpanel.setVisible(true);
                tKey.setEnabled(true);

                break;
          case 3:
                 hillpanel.setVisible(false);
                 tKey.setEnabled(false);
                break;
          case 4:
                 hillpanel.setVisible(false);
                 tKey.setEnabled(false);
                break;
          case 5:
                hillpanel.setVisible(false);
                tKey.setEnabled(true);
                break;
        }




    }//GEN-LAST:event_TipoCifradorActionPerformed

    private void parclActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parclActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_parclActionPerformed

    private void parcfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parcfActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_parcfActionPerformed

    private void ClaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClaveActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ClaveActionPerformed

    private void ClaveBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClaveBActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ClaveBActionPerformed

    private void BotonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAyudaActionPerformed
        // TODO add your handling code here:
        Ayuda a = new Ayuda();
        a.setVisible(true);
    }//GEN-LAST:event_BotonAyudaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Criptoanalisis().setVisible(true);
            }
        });
    }

     public void llenarTabla(int[] coincidencias,char[] caracters ){
        Object[][] data=new Object[26][2];
        Object[] campos={"Caracter","N° coincidencias"};
        for(int i=0;i<26;i++){
            data[i][1]=coincidencias[i];
            data[i][0]=caracters[i];
        }

        tabla.setModel(new DefaultTableModel(data, campos));
    }

     private int[] coincidencias= new int[26];
     private char[] caracters= new char[26];
     // ArrayList<Integer> coincidencias =new ArrayList<Integer>();
     //private ArrayList<String> caracters =new ArrayList<String>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAyuda;
    private javax.swing.JTextField Clave;
    private javax.swing.JTextField ClaveB;
    private javax.swing.JComboBox TipoCifrador;
    private javax.swing.JButton analisis;
    private javax.swing.JPanel hillpanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField parcf;
    private javax.swing.JTextField parcl;
    private javax.swing.JComboBox tKey;
    private javax.swing.JTable tabla;
    private javax.swing.JTextArea tcifrado;
    private javax.swing.JTextArea tplano;
    // End of variables declaration//GEN-END:variables

}
